package com.zhongyi.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;



/**
 * 
 * <p>
 * 
 * 
 * @author <a href="mailto:lysongfie@126.com">Songfei</a>
 * @version 1.0,2012-2-25
 */
public class FileReader {
	
	Sheet sheet;
	private Workbook wkbook;
	private ArrayList<String> worksheets = new ArrayList<String>();
	
	private String fileName;
	private boolean isExcel2007;
	
	Logger logger = Logger.getLogger(FileReader.class);
	
	public void loadFile(String fileName) throws IOException{
		InputStream is = null;
		this.fileName = fileName;
		try {
			is = new FileInputStream(new File(fileName));
			this.loadFile(is);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public void loadFile(InputStream is) throws IOException{
		if (wkbook == null){
            if (this.isExcel2007){
                try {
					wkbook = new XSSFWorkbook(is);
				} catch (IOException e) {
					e.printStackTrace();
				}
            }else{
                POIFSFileSystem excel = null;
                try {
                    excel = new POIFSFileSystem(is);
                } catch (Exception e) {
                    logger.error(
                            " Not a Valid Excel File : " + fileName);
                }
                wkbook = new HSSFWorkbook(excel);
                int noOfWorksheets = wkbook.getNumberOfSheets();
                if (noOfWorksheets != 0) {
                    for (int count = 0; count < noOfWorksheets; ++count) {
                        worksheets.add(wkbook.getSheetName(count));
                    }
                }
            }
        }
	}
	
	public void loadWorkSheet(int index){
		sheet = wkbook.getSheetAt(index);
	}
	
	public Collection<String> getWorkSheets(){
		return worksheets;
	}
	
	
	public List<String> getRow() {
		List<String> dataList = new ArrayList<String>();
        for(Row row : sheet){ 
            for(Cell cell : row){  
                switch(cell.getCellType()){  
                    case Cell.CELL_TYPE_BOOLEAN:  
                        //得到Boolean对象的方法  
                        dataList.add(String.valueOf(cell.getBooleanCellValue()));
                        break;  
                    case Cell.CELL_TYPE_NUMERIC:  
                        //先看是否是日期格式  
                        if(DateUtil.isCellDateFormatted(cell)){  
                            //读取日期格式  
                            dataList.add(String.valueOf(cell.getDateCellValue()));
                        }else{  
                            //读取数字  
                            dataList.add(String.valueOf(cell.getNumericCellValue()));
                        }  
                        break;  
                    case Cell.CELL_TYPE_FORMULA:  
                        //读取公式  
                        dataList.add(cell.getCellFormula());
                        break;  
                    case Cell.CELL_TYPE_STRING:  
                        //读取String  
                        dataList.add(cell.getRichStringCellValue().getString());
                        break;                    
                }  
            } 
        } 
        return dataList;
    }
	
	
}
